package com.android.delivery.util.pay;

import android.content.Context;
import android.util.Log;

import com.android.delivery.constant.Constants;
import com.android.delivery.entity.PayWxInfo;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXPayCommon {
    public static final int PAY_SUCC = 0;
    public static final int PAY_ERROR = -1;
    public static final int PAY_CANCEL = -2;//用户取消
    private Context mContext;

    private static WXPayCommon mStance;
    private IWXAPI api;

    public static WXPayCommon getInstance(Context context) {
        if (mStance == null) {
            mStance = new WXPayCommon(context);
        }
        return mStance;
    }

    private WXPayCommon(Context context) {
        mContext = context;
        api = WXAPIFactory.createWXAPI(mContext, null);
        api.registerApp(Constants.WX_APP_ID);
    }

    public void sendPayReq(PayWxInfo product) {
        PayReq req = new PayReq();
        req.appId = product.appid;
        req.partnerId = product.partnerid;
        req.prepayId = product.prepayid;
        req.packageValue = product.packageValue;
        req.nonceStr = product.noncestr;
        req.timeStamp = product.timestamp;
        req.sign = product.sign;
        boolean result = api.sendReq(req);
        Log.e(WXPayCommon.class.getSimpleName(), "result:" + result);
    }

    public boolean isWeixinAvilible() {
        if (api != null) {
            return api.isWXAppInstalled();
        }
        return true;
    }
}
